package com.xhh.enjoyapp.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.xhh.enjoyapp.Bean.Community;
import com.xhh.enjoyapp.Bean.Post;
import com.xhh.enjoyapp.R;
import com.xhh.enjoyapp.activity.PushCommunity;
import com.xhh.enjoyapp.activity.PushContent;
import com.xhh.enjoyapp.adapter.ChatAdapter;
import com.xhh.enjoyapp.adapter.HomeAdapter;
import com.xhh.enjoyapp.constant.CommonConstant;

import java.util.List;
import java.util.Objects;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * @description 论坛界面 fragment_chat
 * @author: weiXhh
 * @create: 2020-05-14 10:34
 **/
public class FragmentChat extends Fragment {

    private RecyclerView recyclerView;
    private SwipeRefreshLayout refreshLayout;
    private List<Community> data;
    private ChatAdapter recycleViewAdapter;
    private FloatingActionButton fabAdd,fabAddCommunity,fabAddContent;
    private boolean isClickAdd = true;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_chat, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();

        // 初始刷新
        refresh();

        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // 刷新
                refresh();
            }
        });

        // 添加
        fabAdd.setOnClickListener(click->{

            if(isClickAdd){
                isClickAdd = false;
                fabAddContent.setVisibility(View.VISIBLE);
                fabAddCommunity.setVisibility(View.VISIBLE);
            } else {
                isClickAdd = true;
                fabAddContent.setVisibility(View.GONE);
                fabAddCommunity.setVisibility(View.GONE);
            }

        });

        // 发布帖子
        fabAddContent.setOnClickListener(click->{
            startActivity(new Intent(getActivity(), PushContent.class));

        });
        // 添加论坛
        fabAddCommunity.setOnClickListener(click->{
            startActivity(new Intent(getActivity(), PushCommunity.class));
        });
    }

    private void refresh() {
        BmobQuery<Community> cos = new BmobQuery<Community>();
        cos.order("-createdAt");
        cos.setLimit(1000);
        cos.findObjects(new FindListener<Community>() {
            @Override
            public void done(List<Community> list, BmobException e) {
                refreshLayout.setRefreshing(false);

                if(e == null){
                    data = list;
                    recycleViewAdapter = new ChatAdapter(getActivity(),data);
                    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                    recyclerView.setAdapter(recycleViewAdapter);

                } else {
                    Log.e(CommonConstant.FRAGMENT_CHAT, "获取数据失败：" + e);
                    Toast.makeText(getActivity(), "获取数据失败", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    private void initView() {
        recyclerView = Objects.requireNonNull(getActivity()).findViewById(R.id.recycle_view_chat);
        refreshLayout = getActivity().findViewById(R.id.swipe_fresh_chat);
        fabAdd = getActivity().findViewById(R.id.chat_fragment_add);
        fabAddCommunity = getActivity().findViewById(R.id.chat_fragment_add_community);
        fabAddContent = getActivity().findViewById(R.id.chat_fragment_add_content);
    }
}
